Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a set of changes to the way input is handled in Peppercorn.
In short:
parse()
might raise an exception.The recommended usage doesn't mention that
parse()
might raise exceptions.The three places [1] I saw using
parse()
assume it returns sucessfully, so it's easy to feed input to a form that ultimately results in a server error.I don't know if people really expected it to not raise an exception or simply followed the documented example.
The test suite already accounts for
ValueError
. Aditionally,peppercorn.parse()
might raise:RuntimeError
, when the input (even a well-formed one) is nested deeper than the recursion limitStopIteration
when a sequence is started and not closed.Presumably the correct usage would be:
It assumes access to a framework-specific exception, though.
The first commit establishes a
ParseError
, which hopefully makes it easier to target exceptions raised bypeppercorn.parse()
.The second commit takes the documentation at face value ("Mappings and sequences can be nested arbitrarily") and makes it so. This is done by replacing the recursive implementation with one that uses a list as a stack.
Doing this brought on another thing:
peppercorn.parse()
handles input that contains extra end markers by simply returning what was collected up to that point.The non-recursive implementation passed the full test suite even if (as I discoverd later) it handled extra end markers differently.
I added some tests and contorted it a bit to preserve the original behaviour.
I still think that an input like
[('foo', 'fred'), ('__end__', ''), ('bar', 'joe')]
should raise an exception and not be parsed as{'foo': 'fred'}
.This is done in the last commit. I separated the end tests as they might be useful even if you disagree.
[1]
https://github.com/Pylons/deform/blob/master/deform/field.py#L502
https://bitbucket.org/danjac/pyramid_simpleform/src/32d81533f51b/pyramid_simpleform/form.py#cl-107
https://bitbucket.org/ianjosephwilson/pepperedform/src/10b60a55c737/pepperedform/handler.py#cl-16